package com.test.base.service;

import com.test.base.dao.NideshopAddressDao;
import com.test.base.entity.NideshopAddress;
import com.test.base.utils.Assist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AddressService {
    @Autowired
    private NideshopAddressDao nideshopAddressDao;

    public boolean saveOrUpdate(NideshopAddress addr) {
        addr.setCountryId(0);
        int row = 0;
        if (null == addr.getId()) {
            row = nideshopAddressDao.insertNideshopAddress(addr);
        } else {
            Assist region = new Assist();
            region.setRequires(Assist.andEq("user_id", addr.getUserId()));
            region.setRequires(Assist.andEq("id", addr.getId()));
            row = nideshopAddressDao.updateNideshopAddress(addr, region);
        }
        if (addr.getIsDefault() == 1) {
            NideshopAddress updateIsDefat = new NideshopAddress();
            updateIsDefat.setIsDefault(0);
            Assist region = new Assist();
            region.setRequires(Assist.andEq("user_id", addr.getUserId()));
            region.setRequires(Assist.andEq("id", addr.getId()));
            nideshopAddressDao.updateNideshopAddress(updateIsDefat, region);
        }
        if (row == 1) {
            return true;
        } else {
            return false;
        }
    }
}
